<?php

namespace app\circle\controller;


use app\circle\model\CircleModel;
use app\circle\model\MemberModel;
use think\Request;

class MyCircleController extends BaseController
{
    private $circleModel;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->circleModel = new CircleModel();
        if (!$this->islogin()) {
            $this->redirect('Public/userLogin', ['reback' => get_current_url()]);
        }

    }

    /**
     * 我的圈子
     * @return mixed
     */
    public function index()
    {
        $query = $this->request->request();
        $circleName = $this->request->param('circle_name', '');
        $memberCircle = $this->circleModel->getMemberCircle($this->getMemberId());
        $status = $this->request->param('status/d', 0);
        $circleIds = [];
        if ($memberCircle) {
            foreach ($memberCircle as $v) {
                $circleIds[] = $v['circle_id'];
            }
        }
        if ($status) {
            $map['circle_id'] = ['in', $circleIds];
        } else {
            $map['circle_id'] = ['notin', $circleIds];
        }
        if (!empty($circleName)) {
            $map['circle_name'] = ['like', "%$circleName%"];
        }
        $list = $this->circleModel->where($map)->paginate(10, false, ['query' => $query]);
        if ($this->request->isAjax()) {
            return $list;
        }
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 通讯录
     * @return array|bool|mixed
     */
    public function contacts()
    {
        $name = $this->request->param('name', '');
        $companyName = $this->request->param('company_name', '');
        $mobile = $this->request->param('mobile');
        $circleId = $this->request->param('circle_id', 0);
        $model = new MemberModel();
        $list = $model->getContacts($this->getMemberId(), $name, $companyName, $mobile, $circleId);
        if ($this->request->isAjax()) {
            return $list;
        }
        $this->assign('list', $list);
        return $this->fetch();

    }
}